package com.d3works.apps.account.db.repo;

import com.d3works.apps.account.db.DebtRecovery;
import com.d3works.exception.service.TraceBackService;
import javax.persistence.PersistenceException;

public class DebtRecoveryAccountRepository extends DebtRecoveryRepository {

  @Override
  public DebtRecovery save(DebtRecovery debtRecovery) {
    try {
      if (debtRecovery.getAccountingSituation() != null) {
        debtRecovery.setPartner(debtRecovery.getAccountingSituation().getPartner());
      } else if (debtRecovery.getTradingNameAccountingSituation() != null) {
        debtRecovery.setPartner(debtRecovery.getTradingNameAccountingSituation().getPartner());
      }
      return super.save(debtRecovery);
    } catch (Exception e) {
      TraceBackService.traceExceptionFromSaveMethod(e);
      throw new PersistenceException(e.getMessage(), e);
    }
  }
}
